var blockAction = '';
var returnvalue = 0;
var returntext  = '';
$(document).ready(function() {
 	startPage();

	$( window ).resize(function() {
	  startPage();
	});	
	
	$('.search-icon').click(function(e) {
		$('#search_form').submit();
		
        e.preventDefault();
        e.stopPropagation();
    });
	
	 	
	$(document).on({
	    mouseleave: function(){
			$('.timeline-action-box').hide('blind');
	    }
    }, '.timeline-action-box');
    
	$('.action-icon1 , .action-icon2 , .action-icon3').click(function(e) {
	    $(this).find('.action-icon-staus').show('blind');
		
        e.preventDefault();
        e.stopPropagation();
    });	
    	
	$(document).on({
	    mouseleave: function(){
			$(this).hide('blind');
	    }
    }, '.action-icon-staus');	
    
    nextStep(1);

    var d = new Date();
    var toDay = d.getDate() + '/' + (d.getMonth() + 1) + '/' + (d.getFullYear() + 543);

    $( ".date-input" ).datepicker({
        dateFormat: 'dd/mm/yy', isBuddhist: true, defaultDate: toDay, dayNames: ['อาทิตย์', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์'],
        dayNamesMin: ['อา.', 'จ.', 'อ.', 'พ.', 'พฤ.', 'ศ.', 'ส.'],
        monthNames: ['มกราคม', 'กุมภาพันธ์', 'มีนาคม', 'เมษายน', 'พฤษภาคม', 'มิถุนายน', 'กรกฎาคม', 'สิงหาคม', 'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม'],
        monthNamesShort: ['ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'],
      showOn: "button",
      buttonImage: "/images/calendar.png",
      buttonImageOnly: true,
      buttonText: "เลือกวันที่",
        changeMonth: true,  
        changeYear: true      
    });	
    
    $(".formBtn").colorbox({
	    iframe:true,
	    width:"650px",
	    height: "500px",
	    onOpen:function(){ blockAction = $(this).attr('data-block'); returntext = ''; },
		onClosed:function(){ addDatatoHidden(); }
    });
    
    $(".formBtn2").colorbox({
        iframe: true,
        width: "650px",
        height: "500px",
        fastIframe: false,
        onOpen: function () { blockAction = $(this).attr('data-block'); returntext = ''; },
        onClosed: function () { addDatatoHidden2(); }
    });

    $(".formBtn3").colorbox({
        iframe: true,
        width: "650px",
        height: "500px",
        fastIframe: false,
        onOpen: function () { blockAction = $(this).attr('data-block'); returntext = ''; },
        onClosed: function () { addDatatoHidden3(); }
    });

    $(".formBtnArea").colorbox({
        iframe: true,
        width: "650px",
        height: "500px",
        fastIframe: false,
        href: function () { 
            if ($('#country_id')) {
                country_id = $('#country_id').val();
            }
            if ($('#province_id').val()) {
                province_id = $('#province_id').val();
            }
            if ($('#district_id').val()) {
                district_id = $('#district_id').val();
            }
            if ($('#subdistrict_id').val()) {
                subdistrict_id = $('#subdistrict_id').val();
            }
            return '/Complaint/GetCrime?a=crime&country_id=' + country_id + '&province_id=' + province_id + '&district_id=' + district_id + '&subdistrict_id=' + subdistrict_id;
        },
        onOpen: function () { 
            blockAction = $(this).attr('data-block'); returntext = '';
        },
        onClosed: function () { addDatatoHidden2(); }
    });
    
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
		$('.container').css('min-width',1000);
	}    
});
function startPage(){
	var main_menu = $(window).height();
	$('.main_menu').css('min-height',main_menu);
	var search_box = $('.main-logo').height();
	$('.search-box').height(search_box-4);
	$('.search-icon').height(search_box-2);
}
function nextStep(step) {

    if ($('.servicePage_breadcrumbs_box').length > 0) {
        var myClass = $('.servicePage_breadcrumbs_box').eq(step - 1);
        var position = myClass.position();
        var positionLeft = position.left;
        positionLeft += 3;

        $(".servicePage_breadcrumbs_bullte_action").animate({
            left: positionLeft
        }, 1000, "easeInOutExpo", function () {
            switch (step) {
                case 1: $('#servicePageBody').removeClass().addClass("servicePage1-action");; break;
                case 2: $('#servicePageBody').removeClass().addClass("servicePage2-action");; break;
                case 3: $('#servicePageBody').removeClass().addClass("servicePage3-action");; break;
                case 4: $('#servicePageBody').removeClass().addClass("servicePage4-action");; break;
                case 5: $('#servicePageBody').removeClass().addClass("servicePage5-action");; break;
            }

            $('#servicePage-sub-title').html(myClass.find('span').html());

            var myHeight = $('#servicePage').height();
            $('.main_menu').height(myHeight + 20);
        });


        $(".servicePage:visible").hide();
        $("#servicePage" + step).fadeIn('slow');
    }
}


function resetform(){
	window.location.reload();
}
function addDatatoHidden(){
    console.log('active');
	if(returntext != ''){	
		var myarray = [];
		
		$("#block_"+blockAction+" input[type=hidden]").each(function(){
			myarray.push($(this).val());
		});
		if(jQuery.inArray(returnvalue, myarray)!==-1){
		    //console.log("is in array");
		    alert('ข้อมูลนี้ถูกเลือกแล้ว');
		} else {
		    $( "#block_"+blockAction ).append( '<input type="hidden" name="'+blockAction+'_data[]" value="'+returnvalue+'" /><div class="blockReAction" data-value="'+returnvalue+'">'+returntext+'<span onclick="removebox(\''+blockAction+'\','+returnvalue+');"></span></div>' );
		}
	}
}
function addDatatoHidden2() {
    if (returntext != '') {
        $("#block_" + blockAction).find("input").remove();
        $("#block_" + blockAction).find("div").remove();
        $("#block_" + blockAction).append('<input type="hidden" name="' + blockAction + '" value="' + returnvalue + '" /><div class="blockReAction" data-value="' + returnvalue + '">' + returntext + '<span onclick="removebox(\'' + blockAction + '\',' + returnvalue + ');"></span></div>');
   
    }
}
function addDatatoHidden3() {
    if (returntext != '') {
        $("#block_" + blockAction).find("input").remove();
        $("#block_" + blockAction).find("div").remove();
        $("#block_" + blockAction).append(returntext);

    }
}
function removebox(name,id){
	if(confirm("คุณแน่ใจที่จะลบตัวเลือกนี้ ?")){
		$( "#block_"+name ).find("[data-value='" + id + "']").remove();
		$( "#block_"+name ).find("[value='" + id + "']").remove();
	}	
}